package com.dang.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dang.dao.ProductDAO;

import com.dang.model.Book;
import com.dang.model.Product;
import com.dang.util.DBConnection;

public class ProductDAOImp implements ProductDAO{
	
	private final String FIND_BY_ID="select * from d_product p left join d_book b on b.id=p.id where p.id=?";
	
	private final String FIND_BY_HOT="select sum(product_num) as num,p.*,b.*" 
			+ "from d_item i "
			+ "join d_product p "
			+ "ON i.product_id=p.id " 
			+ "JOIN d_book b " 
			+ "ON i.product_id=b.id "
			+ "GROUP BY product_id " 
			+ "ORDER BY num desc LIMIT 0,?";  
	
	private final static String FIND_BY_NEW = "select p.*,b.* from d_product p " 
			+ "join d_book b ON p.id=b.id "
			+ "ORDER BY p.add_time DESC " + "limit 0,?";
	
	public Book findById(int id) throws Exception{
		PreparedStatement prep=null;
		Connection conn=null;
		Book book=null;
		ResultSet rst=null;
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(FIND_BY_ID);
			prep.setInt(1, id);
			rst=prep.executeQuery();
			if(rst.next()){
				book=new Book();
				book.setAuthor(rst.getString("author"));
				book.setPublishing(rst.getString("publishing"));
				book.setId(rst.getInt("id"));
				book.setWordNumber(rst.getString("word_number"));
				book.setPublishTime(rst.getLong("publish_time"));
				book.setWhichEdtion(rst.getString("which_edtion"));
				book.setTotalPage(rst.getString("total_page"));
				book.setPrintTime(rst.getLong("print_time"));
				book.setPrintNumber(rst.getString("print_number"));
				book.setIsbn(rst.getString("isbn"));
				book.setAuthorSummary(rst.getString("author_summary"));
				book.setCatalogue(rst.getString("catalogue"));
				
				book.setProductName(rst.getString("product_name"));
				book.setDescription(rst.getString("description"));
				book.setAddTime(rst.getLong("add_time"));
				book.setFixedPrice(rst.getDouble("fixed_price"));
				book.setDangPrice(rst.getDouble("dang_price"));
				book.setKeywords(rst.getString("keywords"));
				book.setHasDeleted(rst.getInt("has_deleted"));
				book.setProductPic(rst.getString("product_pic"));
			}
			
			}finally{
			   DBConnection.close(rst,prep, conn);
		}
		return book;
	}
	
	public List<Product> findByHot(int num) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		ResultSet rs = null;
		List<Product> pros = new ArrayList<Product>();
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(FIND_BY_HOT);
			prep.setInt(1, num);
			rs = prep.executeQuery();
			while (rs.next()) {
				Book book = new Book();
				book.setAuthor(rs.getString("author"));
				book.setAuthorSummary(rs.getString("author_summary"));
				book.setCatalogue(rs.getString("catalogue"));
				book.setId(rs.getInt("id"));
				book.setIsbn(rs.getString("isbn"));
				book.setPrintNumber(rs.getString("print_number"));
				book.setPrintTime(rs.getLong("print_time"));
				book.setPublishing(rs.getString("publishing"));
				book.setPublishTime(rs.getLong("publish_time"));
				book.setTotalPage(rs.getString("total_page"));
				book.setWhichEdtion(rs.getString("which_edtion"));
				book.setWordNumber(rs.getString("word_number"));

				book.setProductName(rs.getString("product_name"));
				book.setDescription(rs.getString("description"));
				book.setAddTime(rs.getLong("add_time"));
				book.setFixedPrice(rs.getDouble("fixed_price"));
				book.setDangPrice(rs.getDouble("dang_price"));
				book.setKeywords(rs.getString("keywords"));
				book.setHasDeleted(rs.getInt("has_deleted"));
				book.setProductPic(rs.getString("product_pic"));

				pros.add(book);
			}
		} finally {
			DBConnection.close(rs, prep, conn);
		}
		return pros;
	}
	
	public List<Product> findByNew(int num) throws Exception{
		PreparedStatement prep = null;
		Connection conn = null;
		ResultSet rs = null;
		List<Product> pros = new ArrayList<Product>();
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(FIND_BY_NEW);
			prep.setInt(1, num);
			rs = prep.executeQuery();
			while (rs.next()) {
				Book book = new Book();
				book.setAuthor(rs.getString("author"));
				book.setAuthorSummary(rs.getString("author_summary"));
				book.setCatalogue(rs.getString("catalogue"));
				book.setId(rs.getInt("id"));
				book.setIsbn(rs.getString("isbn"));
				book.setPrintNumber(rs.getString("print_number"));
				book.setPrintTime(rs.getLong("print_time"));
				book.setPublishing(rs.getString("publishing"));
				book.setPublishTime(rs.getLong("publish_time"));
				book.setTotalPage(rs.getString("total_page"));
				book.setWhichEdtion(rs.getString("which_edtion"));
				book.setWordNumber(rs.getString("word_number"));

				book.setProductName(rs.getString("product_name"));
				book.setDescription(rs.getString("description"));
				book.setAddTime(rs.getLong("add_time"));
				book.setFixedPrice(rs.getDouble("fixed_price"));
				book.setDangPrice(rs.getDouble("dang_price"));
				book.setKeywords(rs.getString("keywords"));
				book.setHasDeleted(rs.getInt("has_deleted"));
				book.setProductPic(rs.getString("product_pic"));

				pros.add(book);
			}
		} finally {
			DBConnection.close(rs, prep, conn);
		}
		return pros;
	}
}
